﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class user_merchantDal
    {
        /// <summary>
        /// 通过商户查用户
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_user> Querybymerchantid(string merchantid)
        {
            string sql = "select * from ms_user_merchant as mer LEFT JOIN ms_user as users ON mer.user_id=users.id where mer.merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            List<ms_user> uslist = new List<ms_user>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_user user = new ms_user();
                user.username = rd["username"].ToString();
                user.nickname = rd["nickname"].ToString();
                user.head_portrait = rd["head_portrait"].ToString();
                user.integral = rd["integral"].ToString();
                user.id = rd["user_id"].ToString();
                user.balance = rd["balance"].ToString();
                user.register_time = rd["register_time"].ToString();
                uslist.Add(user);
            }
            rd.Close();
            return uslist;

        }
        /// <summary>
        /// 假数据
        /// </summary>
        /// <returns></returns>
        public static List<ms_user> Querybymerchantid1()
        {
            string sql = "select * from ms_user_copy";
            //MySqlParameter[] values = new MySqlParameter[1];
            //values[0] = new MySqlParameter("merchant_id", merchantid);
            List<ms_user> uslist = new List<ms_user>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, null);
            while (rd.Read())
            {
                ms_user user = new ms_user();
                user.username = rd["username"].ToString();
                user.nickname = rd["nickname"].ToString();
                user.head_portrait = rd["head_portrait"].ToString();
                user.integral = rd["integral"].ToString();
                user.id = rd["id"].ToString();
                user.balance = rd["balance"].ToString();
                user.register_time = rd["register_time"].ToString();
                uslist.Add(user);
            }
            rd.Close();
            return uslist;

        }
        /// <summary>
        /// 查昨日用户
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Querynewcount(string merchantid)
        {
            string sql = "select count(*) as count from ms_user_merchant where merchant_id=@merchant_id and to_days(now())-to_days(relation_time)=1";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            int count = 0;
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {

                count = Convert.ToInt32(rd["count"]);
            }
            rd.Close();
            return count;

        }
        
        /// <summary>
        /// 查新用户
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_user> QueryNewuser(string merchantid)
        {
            string sql = "select * from ms_user_merchant as mer LEFT JOIN ms_user as users ON mer.user_id=users.id where mer.merchant_id=@merchant_id and TO_DAYS(mer.relation_time)=TO_DAYS(NOW())";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            List<ms_user> uslist = new List<ms_user>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_user user = new ms_user();
                user.username = rd["username"].ToString();
                user.nickname = rd["nickname"].ToString();
                user.head_portrait = rd["head_portrait"].ToString();
                user.integral = rd["integral"].ToString();
                user.balance = rd["balance"].ToString();
                user.id = rd["user_id"].ToString();
                user.register_time = rd["register_time"].ToString();
                uslist.Add(user);
            }
            rd.Close();
            return uslist;

        }
        /// <summary>
        /// 虚假新数据
        /// </summary>
        /// <returns></returns>
        public static List<ms_user> QueryNewuser1()
        {
            string sql = "select * from ms_user_copy where  TO_DAYS(register_time)=TO_DAYS(NOW())";
            //MySqlParameter[] values = new MySqlParameter[1];
            //values[0] = new MySqlParameter("merchant_id", merchantid);
            List<ms_user> uslist = new List<ms_user>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, null);
            while (rd.Read())
            {
                ms_user user = new ms_user();
                user.username = rd["username"].ToString();
                user.nickname = rd["nickname"].ToString();
                user.head_portrait = rd["head_portrait"].ToString();
                user.integral = rd["integral"].ToString();
                user.balance = rd["balance"].ToString();
                user.id = rd["id"].ToString();
                user.register_time = rd["register_time"].ToString();
                uslist.Add(user);
            }
            rd.Close();
            return uslist;

        }
        /// <summary>
        /// 积分记录
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="merchatid"></param>
        /// <returns></returns>
        public static int Addrecord(string uid, string merchatid)
        {
            string sql = "insert into ms_user_merchant(user_id,merchant_id,relation_time) values(@user_id,@merchant_id,@relation_time)";
            MySqlParameter[] values = new MySqlParameter[3];
            values[0] = new MySqlParameter("user_id", uid);
            values[1] = new MySqlParameter("merchant_id", merchatid);
            values[2] = new MySqlParameter("relation_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, values);
        }
    }
}
